package com.yedean.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yedean.mall.entity.PmsBrand;
import com.yedean.mall.mapper.PmsBrandMapper;
import com.yedean.mall.service.IPmsBrandService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author yedean
 * @since 2022-08-09
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {

    @Override
    public IPage<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PmsBrand::getName, keyword)
                .orderByDesc(PmsBrand::getSort);

        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
        List<PmsBrand> brands = ids.stream().map(id -> {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setId(id);
            pmsBrand.setShowStatus(showStatus);
            return pmsBrand;
        }).collect(Collectors.toList());

        return this.updateBatchById(brands);
    }

    @Override
    public boolean updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        List<PmsBrand> brands = ids.stream().map(id -> {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setId(id);
            pmsBrand.setShowStatus(factoryStatus);
            return pmsBrand;
        }).collect(Collectors.toList());

        return this.updateBatchById(brands);
    }
}
